

import { resolve } from 'node:path'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// 图标
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

import topLevelAwait from 'vite-plugin-top-level-await';

import type { UserConfig, ConfigEnv } from 'vite';
import { loadEnv } from 'vite';

const CWD = process.cwd();

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  // 环境变量
  const { VITE_BASE_URL, VITE_DROP_CONSOLE, VITE_MOCK_IN_PROD } = loadEnv(mode, CWD);

  const isDev = command === 'serve';
  const isBuild = command === 'build';

  return {
    base: "./",
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, './src'),
        },
      ],
    },
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      topLevelAwait(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [resolve(CWD, 'src/assets/icons')],
        // Specify symbolId formatc
        symbolId: 'svg-icon-[dir]-[name]'
      })
    ],
    build: {
      minify: 'esbuild',
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          manualChunks(id) {
            //TODO fix circular imports
            if (id.includes('/src/locales/helper.ts')) {
              return 'antdv'
            } else if (id.includes('node_modules/ant-design-vue/')) {
              return 'antdv'
            } else if (/node_modules\/(vue|vue-router|pinia)\//.test(id)) {
              return 'vue'
            }
          }
        },
        onwarn(warning, rollupWarn) {
          // ignore circular dependency warning
          if (
            warning.code === 'CYCLIC_CROSS_CHUNK_REEXPORT' &&
            warning.exporter?.includes('src/api/')
          ) {
            return
          }
          rollupWarn(warning)
        }
      }
    },

    server:{
      host: true,


    }
  };
};
